

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    
    <title>pyamp.system.ubuntu.menu &mdash; pyamp v1.2 documentation</title>
    <link rel="stylesheet" href="../../../../_static/default.css" type="text/css" />
    <link rel="stylesheet" href="../../../../_static/pygments.css" type="text/css" />
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    '../../../../',
        VERSION:     '1.2',
        COLLAPSE_INDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  true
      };
    </script>
    <script type="text/javascript" src="../../../../_static/jquery.js"></script>
    <script type="text/javascript" src="../../../../_static/underscore.js"></script>
    <script type="text/javascript" src="../../../../_static/doctools.js"></script>
    <link rel="top" title="pyamp v1.2 documentation" href="../../../../index.html" />
    <link rel="up" title="pyamp.system.ubuntu" href="../ubuntu.html" /> 
  </head>
  <body>
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../../../../genindex.html" title="General Index"
             accesskey="I">index</a></li>
        <li class="right" >
          <a href="../../../../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li><a href="../../../../index.html">pyamp v1.2 documentation</a> &raquo;</li>
          <li><a href="../../../index.html" >Module code</a> &raquo;</li>
          <li><a href="../ubuntu.html" accesskey="U">pyamp.system.ubuntu</a> &raquo;</li> 
      </ul>
    </div>  

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body">
            
  <h1>Source code for pyamp.system.ubuntu.menu</h1><div class="highlight"><pre>
<span class="c"># Copyright 2012 Brett Ponsler</span>
<span class="c"># This file is part of pyamp.</span>
<span class="c">#</span>
<span class="c"># pyamp is free software: you can redistribute it and/or modify</span>
<span class="c"># it under the terms of the GNU General Public License as published by</span>
<span class="c"># the Free Software Foundation, either version 3 of the License, or</span>
<span class="c"># (at your option) any later version.</span>
<span class="c">#</span>
<span class="c"># pyamp is distributed in the hope that it will be useful,</span>
<span class="c"># but WITHOUT ANY WARRANTY; without even the implied warranty of</span>
<span class="c"># MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the</span>
<span class="c"># GNU General Public License for more details.</span>
<span class="c">#</span>
<span class="c"># You should have received a copy of the GNU General Public License</span>
<span class="c"># along with pyamp.  If not, see &lt;http://www.gnu.org/licenses/&gt;.</span>
<span class="sd">&#39;&#39;&#39;The menu module contains the Menu class which provides the ability to locate</span>
<span class="sd">the configuration file for the Ubuntu application menu, as well as locating the</span>
<span class="sd">system directory containing the applications.</span>

<span class="sd">&#39;&#39;&#39;</span>
<span class="kn">from</span> <span class="nn">os</span> <span class="kn">import</span> <span class="n">environ</span>
<span class="kn">from</span> <span class="nn">os.path</span> <span class="kn">import</span> <span class="n">exists</span><span class="p">,</span> <span class="n">join</span>


<span class="n">__all__</span> <span class="o">=</span> <span class="p">[</span><span class="s">&quot;Menu&quot;</span><span class="p">]</span>


<div class="viewcode-block" id="Menu"><a class="viewcode-back" href="../../../../_generated/pyamp_system_ubuntu_menu.html#pyamp.system.ubuntu.menu.Menu">[docs]</a><span class="k">class</span> <span class="nc">Menu</span><span class="p">:</span>
    <span class="sd">&#39;&#39;&#39;The Menu class provides the ability to find the file containing</span>
<span class="sd">    the configuration data pertaining to the Ubuntu applications menu, as</span>
<span class="sd">    well as the ability to locate the directory which stores the specific</span>
<span class="sd">    application configuration files.</span>

<span class="sd">    &#39;&#39;&#39;</span>

    <span class="c"># A tuple containing the directories and final filename for</span>
    <span class="c"># the applications menu file to locate</span>
    <span class="n">MenuFilename</span> <span class="o">=</span> <span class="p">(</span><span class="s">&quot;menus&quot;</span><span class="p">,</span> <span class="s">&quot;applications.menu&quot;</span><span class="p">)</span>

    <span class="c"># Get the home directory location</span>
    <span class="n">Home</span> <span class="o">=</span> <span class="n">environ</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s">&quot;HOME&quot;</span><span class="p">)</span>

    <span class="c"># Get the XDG_CONFIG_HOME environment variable, defaulting to the</span>
    <span class="c"># ~/.config directory if it is not set</span>
    <span class="n">ConfigHome</span> <span class="o">=</span> <span class="n">environ</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s">&quot;XDG_CONFIG_HOME&quot;</span><span class="p">,</span> <span class="n">join</span><span class="p">(</span><span class="n">Home</span><span class="p">,</span> <span class="s">&quot;.config&quot;</span><span class="p">))</span>

    <span class="c"># Get the XDG_CONFIG_DIRS environment variable, defaulting to the</span>
    <span class="c"># /etc/xdg directory if it is not set</span>
    <span class="n">ConfigDirs</span> <span class="o">=</span> <span class="n">environ</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s">&quot;XDG_CONFIG_DIRS&quot;</span><span class="p">,</span> <span class="n">join</span><span class="p">(</span><span class="s">&quot;/etc&quot;</span><span class="p">,</span> <span class="s">&quot;xdg&quot;</span><span class="p">))</span>

    <span class="c"># Get the XDG_DATA_DIRS environment variable, defaulting to the</span>
    <span class="c"># /usr/share directory if it is not set</span>
    <span class="n">DataDirs</span> <span class="o">=</span> <span class="n">environ</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s">&quot;XDG_DATA_DIRS&quot;</span><span class="p">,</span> <span class="n">join</span><span class="p">(</span><span class="s">&quot;/usr&quot;</span><span class="p">,</span> <span class="s">&quot;share&quot;</span><span class="p">))</span>

    <span class="nd">@classmethod</span>
<div class="viewcode-block" id="Menu.findMenuFile"><a class="viewcode-back" href="../../../../_generated/pyamp_system_ubuntu_menu.html#pyamp.system.ubuntu.menu.Menu.findMenuFile">[docs]</a>    <span class="k">def</span> <span class="nf">findMenuFile</span><span class="p">(</span><span class="n">cls</span><span class="p">):</span>
        <span class="sd">&#39;&#39;&#39;Return the system file associated with the applications menu, or</span>
<span class="sd">        None if it was unable to be located.</span>

<span class="sd">        &#39;&#39;&#39;</span>
        <span class="c"># How to locate the applications menu file:</span>
        <span class="c"># 1. Search each directory in $XDG_CONFIG_HOME in order to find</span>
        <span class="c">#    menus/applications.menu. If $XDG_CONFIG_HOME is not set, it</span>
        <span class="c">#    defaults to the ~/.config/ directory.</span>
        <span class="c"># 2. Search each directory in $XDG_CONFIG_DIRS in order to find</span>
        <span class="c">#    menus/applications.menu. If $XDG_CONFIG_DIRS is not set, it</span>
        <span class="c">#    defaults to the /etc/xdg/ directory.</span>
        <span class="c"># 3. Use the first applications.menu file found.</span>

        <span class="n">order</span> <span class="o">=</span> <span class="n">cls</span><span class="o">.</span><span class="n">ConfigHome</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s">&quot;:&quot;</span><span class="p">)</span> <span class="o">+</span> <span class="n">cls</span><span class="o">.</span><span class="n">ConfigDirs</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s">&quot;:&quot;</span><span class="p">)</span>

        <span class="k">for</span> <span class="n">directory</span> <span class="ow">in</span> <span class="n">order</span><span class="p">:</span>
            <span class="n">applicationFile</span> <span class="o">=</span> <span class="n">join</span><span class="p">(</span><span class="n">directory</span><span class="p">,</span> <span class="o">*</span><span class="n">cls</span><span class="o">.</span><span class="n">MenuFilename</span><span class="p">)</span>
            <span class="k">if</span> <span class="n">exists</span><span class="p">(</span><span class="n">applicationFile</span><span class="p">):</span>
                <span class="k">return</span> <span class="n">applicationFile</span>

        <span class="k">return</span> <span class="bp">None</span>
</div>
    <span class="nd">@classmethod</span>
<div class="viewcode-block" id="Menu.findApplicationsDirectory"><a class="viewcode-back" href="../../../../_generated/pyamp_system_ubuntu_menu.html#pyamp.system.ubuntu.menu.Menu.findApplicationsDirectory">[docs]</a>    <span class="k">def</span> <span class="nf">findApplicationsDirectory</span><span class="p">(</span><span class="n">cls</span><span class="p">):</span>
        <span class="sd">&#39;&#39;&#39;Return the directory which contains the application desktop files,</span>
<span class="sd">        or None if it was unable to be located.</span>

<span class="sd">        &#39;&#39;&#39;</span>
        <span class="c"># Get the base directory for applications from XDG_DATA_DIRS</span>
        <span class="c"># if that is not set, then default to /usr/share</span>
        <span class="k">for</span> <span class="n">directory</span> <span class="ow">in</span> <span class="n">Menu</span><span class="o">.</span><span class="n">DataDirs</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s">&quot;:&quot;</span><span class="p">):</span>
            <span class="n">applicationsDir</span> <span class="o">=</span> <span class="n">join</span><span class="p">(</span><span class="n">directory</span><span class="p">,</span> <span class="s">&quot;applications&quot;</span><span class="p">)</span>
            <span class="k">if</span> <span class="n">exists</span><span class="p">(</span><span class="n">applicationsDir</span><span class="p">):</span>
                <span class="k">return</span> <span class="n">applicationsDir</span>

        <span class="k">return</span> <span class="bp">None</span></div></div>
</pre></div>

          </div>
        </div>
      </div>
      <div class="sphinxsidebar">
        <div class="sphinxsidebarwrapper">
<div id="searchbox" style="display: none">
  <h3>Quick search</h3>
    <form class="search" action="../../../../search.html" method="get">
      <input type="text" name="q" size="18" />
      <input type="submit" value="Go" />
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
    <p class="searchtip" style="font-size: 90%">
    Enter search terms or a module, class or function name.
    </p>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
      <div class="clearer"></div>
    </div>
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../../../../genindex.html" title="General Index"
             >index</a></li>
        <li class="right" >
          <a href="../../../../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li><a href="../../../../index.html">pyamp v1.2 documentation</a> &raquo;</li>
          <li><a href="../../../index.html" >Module code</a> &raquo;</li>
          <li><a href="../ubuntu.html" >pyamp.system.ubuntu</a> &raquo;</li> 
      </ul>
    </div>
    <div class="footer">
        &copy; Copyright 2012, Brett Ponsler.
      Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.0.1.
    </div>
  </body>
</html>